package com.zf.shuashua.advert.mg;

import android.text.TextUtils;

import androidx.appcompat.app.AppCompatActivity;

import com.zf.shuashua.ShuaApplication;
import com.zf.shuashua.manager.CalendarManager;
import com.zf.shuashua.net.Api;

import java.util.List;

public class MGReportManager {

    private static MGReportManager instance;

    private MGReportManager() {

    }

    public static MGReportManager getInstance() {
        if (null == instance) {
            synchronized (MGReportManager.class) {
                if (null == instance) {
                    instance = new MGReportManager();
                }
            }
        }
        return instance;
    }

    public void startReport(ReportLocationType reportLocationType, AdDataBean mAdDataBean) {
        startReport(reportLocationType,mAdDataBean, "", "", "", "", "", "", "", "");
    }

    private List<String> getReportApiList(ReportLocationType reportLocationType, AdDataBean mAdDataBean) {
        AdDataBean.AdInfoBean.AdBaseInfoBean.ConvUrlBean convUrl = mAdDataBean.getAdInfo().get(0).getAdBaseInfo().getConvUrl();
        switch (reportLocationType) {
            case Show:
                return convUrl.getShow();
            case Click:
                return convUrl.getClick();
            case startDown:
                return convUrl.getStartDown();
            case endDown:
                return convUrl.getEndDown();
            case stopDown:
                return convUrl.getStopDown();
            case continueDown:
                return convUrl.getContinueDown();
            case deleteDown:
                return convUrl.getDeleteDown();
            case startInstall:
                return convUrl.getStartInstall();
            case endInstall:
                return convUrl.getEndInstall();
            case activation:
                return convUrl.getActivation();
            case startPlay:
                return convUrl.getStartPlay();
            case endPlay:
                return convUrl.getEndPlay();
            case stopPlay:
                return convUrl.getStopPlay();
            case closeAd:
                return convUrl.getCloseAd();
            case deeplinkClick:
                return convUrl.getDeeplinkClick();
            default:
                return null;
        }
    }

    private void startReport(
            final ReportLocationType reportLocationType,
            AdDataBean mAdDataBean,
            String clickDownX,
            String clickDownY,
            String clickUpX,
            String clickUpY,
            String clickDownX1,
            String clickDownY1,
            String clickUpX1,
            String clickUpY1
    ) {

        List<String> reportApiList = getReportApiList(reportLocationType, mAdDataBean);

        if (reportApiList != null && !reportApiList.isEmpty()) {
            for (String url : reportApiList) {
                if (!TextUtils.isEmpty(url)) {

                    String newUrl = replaceUrl(mAdDataBean, url, clickDownX, clickDownY, clickUpX, clickUpY, clickDownX1, clickDownY1, clickUpX1, clickUpY1);
                    MLog.logi("startReport", newUrl);
                    Api.reportUrl(newUrl, new Api.CommonCallback<Object>() {
                        @Override
                        public void onSuccess(Object s) {
                            MLog.logd(reportLocationType.getName() + ": API汇报成功：" + s);
                        }

                        @Override
                        public void onError(String errMsg) {
                            MLog.loge(reportLocationType.getName() + ": API汇报失败：" + errMsg);
                        }
                    });
                }
            }
        }
    }

    private String replaceUrl(
            AdDataBean mAdDataBean,
            String originUrl,
            String clickDownX,
            String clickDownY,
            String clickUpX,
            String clickUpY,
            String clickDownX1,
            String clickDownY1,
            String clickUpX1,
            String clickUpY1
    ) {

        long timeLong = System.currentTimeMillis();
        originUrl = originUrl.replace("__TS__", String.valueOf(timeLong));
        originUrl = originUrl.replace("__TS1__", String.valueOf((int) (timeLong / 1000)));
        originUrl = originUrl.replace("__clickDownX__", clickDownX);
        originUrl = originUrl.replace("__clickDownY__", clickDownY);
        originUrl = originUrl.replace("__clickUpX__", clickUpX);
        originUrl = originUrl.replace("__clickUpY__", clickUpY);
        originUrl = originUrl.replace("__clickDownX1__", clickDownX1);
        originUrl = originUrl.replace("__clickDownY1__", clickDownY1);
        originUrl = originUrl.replace("__clickUpX1__", clickUpX1);
        originUrl = originUrl.replace("__clickUpY1__", clickUpY1);
        try{
            String appDownloadUrl = mAdDataBean.getAdInfo().get(0).getAdConversionInfo().getAppDownloadUrl();
            if (!TextUtils.isEmpty(appDownloadUrl) && ShuaApplication.gdtClickIdMap != null && ShuaApplication.gdtClickIdMap.containsKey(appDownloadUrl)) {
                String clickId = ShuaApplication.gdtClickIdMap.get(appDownloadUrl);
                originUrl = originUrl.replace("__CLICK_ID__", clickId);
            }
        }catch (Exception e){

        }
        return originUrl;
    }

}
